package org.invigorare.auctions.data;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.*;

import org.invigorare.auctions.data.entities.EntryInfo;

public class EntryDataOpener extends SynchronizableInboundDataOpener {

	private static final int DATABASE_VERSION = 8;

	public static final String ENTRY_TABLE_NAME 	= "entry_table";
	public static final String ENTRY_TABLE_NULL_COL	= "nullIfNoData";	
	
	public static final String _ID				= "_id"; // Internal Android rowId: Column Name must be exactly so, for Query Adapters to work
	public static final String ENTRY_ID			= "entry_id";
	public static final String TIMESTAMP		= "timestamp"; 
	public static final String DRIVER_NAME		= "driver_name"; 
	public static final String VEHICLE_NO		= "vehicle_no"; 
	public static final String VEHICLE_TYPE		= "vehicle_type";
	
	public static final String PRODUCE_ID		= "produce_id";
	public static final String PRODUCE_WEIGHT	= "produce_weight";
	public static final String PRODUCE_FARM		= "produce_farm";
	
	public static final String BEARER_LIC		= "bearer_lic";
	public static final String AGENT_LIC		= "comm_agent_lic";
	// fees, emp id
	
	private static final ColBinding[] columnBindings = new ColBinding[] {
		new ColBinding(_ID, 					SqlTypes.INT_KEY),
		
		new ColBinding(ENTRY_ID, 				SqlTypes.TEXT),
		new ColBinding(TIMESTAMP, 				SqlTypes.TEXT),
		new ColBinding(DRIVER_NAME,				SqlTypes.TEXT),
		new ColBinding(VEHICLE_NO,				SqlTypes.TEXT),
		new ColBinding(VEHICLE_TYPE,			SqlTypes.TEXT),
		
		new ColBinding(PRODUCE_ID,				SqlTypes.INT),
		new ColBinding(PRODUCE_WEIGHT,			SqlTypes.REAL),
		new ColBinding(PRODUCE_FARM,			SqlTypes.TEXT),
		
		new ColBinding(BEARER_LIC,				SqlTypes.TEXT),
		new ColBinding(AGENT_LIC,				SqlTypes.TEXT),
		
	};
	
	public EntryDataOpener(Context context) {
		super(context, ENTRY_TABLE_NAME, null, DATABASE_VERSION, ENTRY_TABLE_NULL_COL);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(createTableString(ENTRY_TABLE_NAME, columnBindings));
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
		// TODO right now, just discards the old db and makes a new one
		db.execSQL("DROP TABLE " + ENTRY_TABLE_NAME);
		db.execSQL(createTableString(ENTRY_TABLE_NAME, columnBindings));
	}
	
	public static SQLiteDatabase getWritableDb(Context context) {
		EntryDataOpener dbHelper = new EntryDataOpener(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		return db;
	}
	
	public static SQLiteDatabase getReadableDb(Context context) {
		EntryDataOpener dbHelper = new EntryDataOpener(context);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		return db;
	}
	
	/** Opens the Entry Table from the database and returns 
	 * a Map of names of the produce types, in the language specified.
	 * 
	 * @param context the Application Context containing the desired database
	 * @param languageCode
	 * @return a map of the entry Ids (string) to struct EntryData.
	 */
	public static Map<String, EntryInfo> getEntryIds(Context context) {
		Map<String, EntryInfo> entries = new HashMap<String, EntryInfo>();
		
		SQLiteDatabase db = getReadableDb(context);
		Log.i("EntryDataOpener", "Opening DB to get list of entry ids.");
		
		// The Columns we want to fetch
		final String[] columns = new String[] {
			ENTRY_ID, TIMESTAMP, DRIVER_NAME, VEHICLE_NO, VEHICLE_TYPE, PRODUCE_ID, PRODUCE_WEIGHT, PRODUCE_FARM, BEARER_LIC, AGENT_LIC};
		
		// Pull all the data and put it into the desired data structure
		Cursor query = db.query(ENTRY_TABLE_NAME, columns, null, null, null, null, null);

		while (query.moveToNext()) {
			String id = query.getString(0);
			String time = query.getString(1);
			String dName = query.getString(2);
			String vNo = query.getString(3);
			String vType = query.getString(4);
			int prodId = query.getInt(5);
			float prodW = query.getFloat(6);
			String prodFarm = query.getString(7);
			String bearerLic = query.getString(8);
			String agentLic = query.getString(9);
			entries.put(id, new EntryInfo(id, time, dName, vNo, vType, prodId, prodW, prodFarm, bearerLic, agentLic));
		}
		
		// Cleanup and return
		query.close();
		db.close();
		return entries;
	}
}
